package com.thinkcube.edutainment.adapters;

import java.util.ArrayList;
import java.util.List;

import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.EBean;
import org.androidannotations.annotations.RootContext;
import org.androidannotations.annotations.Trace;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.rest.RestService;
import org.androidannotations.annotations.sharedpreferences.Pref;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.Toast;

import com.thinkcube.edutainment.EdutainmentConstant;
import com.thinkcube.edutainment.ReachabilityTest;
import com.thinkcube.edutainment.ReplyActivity_;
import com.thinkcube.edutainment.client.NotificationClient;
import com.thinkcube.edutainment.client.UserClient;
import com.thinkcube.edutainment.domain.MetaData;
import com.thinkcube.edutainment.domain.OAuth;
import com.thinkcube.edutainment.domain.data.MessageData;
import com.thinkcube.edutainment.domain.dto.CourseDto;
import com.thinkcube.edutainment.domain.dto.OAuthDto;
import com.thinkcube.edutainment.service.SPreferences_;
import com.thinkcube.edutainment.service.viewitem.MessageItem;
import com.thinkcube.edutainment.service.viewitem.MessageItem_;

@EBean
public class MessageAdapter extends BaseAdapter {

	List<MessageData> messageData;

	@RootContext
	Context context;

	@Override
	public View getView(final int position, final View convertView,
			final ViewGroup parent) {

		MessageItem messageItem;
		if (convertView == null) {
			messageItem = MessageItem_.build(context);
		} else {
			messageItem = (MessageItem) convertView;
		}

		messageItem.bind(getItem(position));

		Button deleteBtn = messageItem.getDelete();

		Button replyBtn = messageItem.getReply();

		replyBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				Intent intent = new Intent(context, ReplyActivity_.class);
				// Bundle bundle = new Bundle();
				// bundle.putParcelable("meessagedata", getItem(position));
				intent.putStringArrayListExtra("message",
						(ArrayList<String>) getItem(position).getUsers());

				context.startActivity(intent);
			}
		});

		deleteBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// for delete
				gettoken(getItem(position).getId(), position);
			}
		});

		return messageItem;
	}

	public List<MessageData> getMessageData() {
		return messageData;
	}

	public void setMessageData(List<MessageData> messageData) {
		this.messageData = messageData;
	}

	@Override
	public int getCount() {
		return messageData.size();
	}

	@Override
	public MessageData getItem(final int position) {
		return messageData.get(position);
	}

	@Override
	public long getItemId(final int position) {
		return position;
	}

	@Bean
	OAuthDto oAuthDto;

	@RestService
	UserClient userClient;

	@RestService
	NotificationClient notificationClient;

	@Trace
	@Background
	void gettoken(final String id, final int position) {
		oAuthDto.setAppId(EdutainmentConstant.APP_ID);
		oAuthDto.setAppSecret(EdutainmentConstant.APP_SCRET);

		new ReachabilityTest(context, new ReachabilityTest.Callback() {

			@Override
			public void onReachabilityTestPassed() {

				try {
					delete(userClient.getRequestToken(oAuthDto
							.getMultiValuedMap()), id, position);
				} catch (Exception e) {

					showMessge(e.getLocalizedMessage());
				}
			}

			@Override
			public void onReachabilityTestFailed() {
			
				showMessge("not connected ");
			}
		});
	}

	@UiThread
	void showMessge(final String msg) {
		Toast.makeText(context, msg, Toast.LENGTH_LONG).show();
	}

	@Bean
	CourseDto courseDto;

	@Pref
	SPreferences_ pref;

	@Trace
	@Background
	void delete(OAuth requestToken, String id, final int position) {
		courseDto.setAuthToken(requestToken.getoAuth().getToken());

		courseDto.setUserId(pref.getId().get());
		courseDto.setMsgd(id);
		new ReachabilityTest(context, new ReachabilityTest.Callback() {

			@Override
			public void onReachabilityTestPassed() {

				try {
					delete(notificationClient.delete(courseDto
							.getMultiValuedMap()), position);
				} catch (Exception e) {

					// showMessge(e.getLocalizedMessage());
				}
			}

			@Override
			public void onReachabilityTestFailed() {

				showMessge("not connected ");
			}
		});
	}

	void delete(MetaData delete, int position) {
		messageData.remove(position);
		notifyDataSetChanged();
		showMessge("" + delete.getMessageText());
	}

}
